package ru.agiledev.akb.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import ru.agiledev.akb.client.panel.*;
import ru.agiledev.akb.shared.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AspectKnowledgeBaseApplication implements EntryPoint{
    /**
     * The message displayed to the user when the server cannot be reached or
     * returns an error.
     */
    private static final String SERVER_ERROR = "An error occurred while "
            + "attempting to contact the server. Please check your network "
            + "connection and try again.";

    private static AspectKnowledgeBaseApplication threadLocal;
    private User user;
    private LoginPanel login;
    private FooterPanel footer;
    private TabLayoutPanel mainPanel;
    private ExamplePanel examplesPanel;
    private PDFInfoPanel infoPanel;
    private InputPanel inputPanel;

    public static AspectKnowledgeBaseApplication get(){
        return threadLocal;
    }
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        threadLocal = this;
        mainPanel = new TabLayoutPanel(25, Style.Unit.PX);



        login = new LoginPanel();

        RootPanel.get("login").add(login);

        //Examples panel
        examplesPanel = new ExamplePanel();

        //Info panel with help image
        infoPanel = new PDFInfoPanel();

        //
        mainPanel.add(infoPanel, "Информация");
        mainPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
                int i = integerSelectionEvent.getSelectedItem();
                if(i==1){
                    if(mainPanel.getWidget(i) instanceof ExamplePanel){
                        ((ExamplePanel)mainPanel.getWidget(i)).setDefaultValues();
                    }

                }
            }
        });

        mainPanel.add(examplesPanel, "Примеры");

        mainPanel.setHeight("780px");
        mainPanel.setWidth("800px");


        HTML name = new HTML("<h1>Аспектно-Ориентированная База Знаний</h1>");

        RootPanel.get("content").add(name);
        RootPanel.get("content").add(mainPanel);

        footer = new FooterPanel();
        RootPanel.get("footer").add(footer);

    }

    public void setUser(User user) {
        if((user!=null)&&(getUser()==null)){
            if((login.getRegisterWindow()!=null)&&login.getRegisterWindow().isVisible()){
                login.getRegisterWindow().hide();
            }

            this.user = user;

            login.removeFromParent();

            //headerPanel.add(new HelloPanel(user.getFirstName()));
            RootPanel.get("login").add(new HelloPanel(user.getFirstName()));

            //Vertical panel with main fields
            inputPanel = new InputPanel();
            mainPanel.add(inputPanel, "Ввод данных");
        } else if((user!=null)&&(getUser()!=null)){
            this.user = user;
             RootPanel.get("login").getWidget(0).removeFromParent();
            RootPanel.get("login").add(new HelloPanel(user.getFirstName()));

        } else {
            mainPanel.remove(2);
            RootPanel.get("login").remove(0);
            login.clear();
            RootPanel.get("login").add(login);
            this.user = null;
        }

    }

    public User getUser() {
        return user;
    }

    public void showExamples(){
        mainPanel.selectTab(1);
    }
}
